package com.edata.bigdata.nebulasource

import org.apache.log4j.Logger

class NebulaWriteConfig extends Serializable {
  @transient lazy val LOGGER = Logger.getLogger(this.getClass)
  var space = "edata"
  var label = "edata"
  var label_type = "vertex" //vertex or edge
  var gid_field = "" //field of "srcId,dstId" or "vid"
  var batch = 512
  var write_mode = "insert" //insert or update
  var overwrite = true
  var schema = "A|A1,B|B1,C|C1"
  var gidAsProp = false

  def withSpace(space: String): NebulaWriteConfig = {
    this.space = space
    this
  }

  def withLabel(label: String): NebulaWriteConfig = {
    this.label = label
    this
  }

  def withLabelType(label_type: String): NebulaWriteConfig = {
    this.label_type = label_type
    this
  }

  def withGIDField(gid_field: String): NebulaWriteConfig = {
    this.gid_field = gid_field
    this
  }

  def withBatch(batch: Int): NebulaWriteConfig = {
    this.batch = batch
    this
  }

  def withWriteMode(write_mode: String): NebulaWriteConfig = {
    this.write_mode = write_mode.toLowerCase()
    this
  }

  def withOverwrite(overwrite: Boolean): NebulaWriteConfig = {
    this.overwrite = overwrite
    this
  }

  def withSchema(schema:String):NebulaWriteConfig = {
    this.schema = schema
    this
  }

  def withGIDAsProp(gidAsProp:Boolean):NebulaWriteConfig={
    this.gidAsProp = gidAsProp
    this
  }

  def build(): NebulaWriteConfig = {
    check()
    this
  }

  def check(): Unit = {
    assert(space != null && !space.isEmpty, "space is empty")
    assert(label != null && !label.isEmpty, "label is empty")
    assert(batch > 0, "batch must be larger than 0")
    if (write_mode.equals("insert") && overwrite) {
      LOGGER.warn(s"only write_mode is 'insert' and that overwrite is validate")
    }
    if (label_type.equals("vertex") && gid_field.split(",").size == 1) {
      LOGGER.warn(s"label_type is 'vertex', only one field can be available as VID")
    }
    if (label_type.equals("edge") && gid_field.split(",").size == 2) {
      LOGGER.warn(s"label_type is 'edge', two field is needed as SID and TID")
    }
  }

}

object NebulaWriteConfig {
  def builder(): NebulaWriteConfig = {
    new NebulaWriteConfig
  }
}
